/*
 *  ResourceManager.mm
 *  NeHe.iOS.Lesson06
 *
 *  Created by BongjuKim on 11. 3. 3..
 *  Copyright 2011 thinkware. All rights reserved.
 *
 */


#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <string>
#import <iostream>
#import "Interfaces.hpp"


using namespace std;

class ResourceManager : public IResourceManager {
public:
	ResourceManager()
	{
		m_imageData = 0;
	}
	string GetResourcePath() const
	{
		NSString* bundlePath = [[NSBundle mainBundle] resourcePath];
		return [bundlePath UTF8String];
	}
	
	TextureDescription LoadImage(const string& filename)
    {
        NSString* basePath = [NSString stringWithUTF8String:filename.c_str()];
        NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
        NSString* fullPath = [resourcePath stringByAppendingPathComponent:basePath];
        UIImage* uiImage = [UIImage imageWithContentsOfFile:fullPath];
		
        TextureDescription description;
        description.Size.x = CGImageGetWidth(uiImage.CGImage);
        description.Size.y = CGImageGetHeight(uiImage.CGImage);
        description.BitsPerComponent = 8;
        description.Format = TextureFormatRgba;
        description.MipCount = 1;
		
        int bpp = description.BitsPerComponent / 2;
        int byteCount = description.Size.x * description.Size.y * bpp;
        unsigned char* data = (unsigned char*) calloc(byteCount, 1);
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big;
        CGContextRef context = CGBitmapContextCreate(data,
													 description.Size.x,
													 description.Size.y,
													 description.BitsPerComponent,
													 bpp * description.Size.x,
													 colorSpace,
													 bitmapInfo);
        CGColorSpaceRelease(colorSpace);
        CGRect rect = CGRectMake(0, 0, description.Size.x, description.Size.y);
        CGContextDrawImage(context, rect, uiImage.CGImage);
        CGContextRelease(context);
        
        m_imageData = [NSData dataWithBytesNoCopy:data length:byteCount freeWhenDone:YES];
        return description;
    }
	
	FILE* LoadTextData(const string& filename)
	{
		NSString* path = [[NSBundle mainBundle] pathForResource:@"f15" ofType:@"data"];
		
		NSString* basePath = [NSString stringWithUTF8String:filename.c_str()];
		NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
        NSString* fullPath = [resourcePath stringByAppendingPathComponent:basePath];

		
		FILE* fp = fopen([path cStringUsingEncoding:1], "r");
		
		if (fp == NULL) {
			NSLog([path	stringByAppendingString:@"not found"]);
		}
		
		return fp;
	}
	
	void* GetImageData()
    {
		if (m_imageData != 0)
			return (void*) [m_imageData bytes];
		
		return NULL;
    }
    void UnloadImage()
    {
        m_imageData = 0;
    }
	
private:
	NSData* m_imageData;

};

IResourceManager* CreateResourceManager()
{
	return new ResourceManager();
}


